#!/usr/bin/env bash
#set -x
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

scriptdir="`dirname "$0"`"
sbin="`cd "$scriptdir"/../../sbin; pwd`"

green='\e[0;32m'
yellow='\e[0;33m'

if [ -n "$2" ]; then
  APP_NAME="$1"
  SNAPPY_EXAMPLE_CLASS="$1"
  shift
  LEADURL=$1
else
  echo "Usage: ./quickstart/scripts/run-streaming-example <example-class> <lead url>" 1>&2
  echo "  - set APP_PROPS=XX to use for twitter credentials" 1>&2
  echo "  - can use abbreviated example class name relative to io.snappydata.examples" 1>&2
  echo "   (e.g. ./quickstart/scripts/run-streaming-example TwitterPopularTagsJob localhost:8090 )" 1>&2
  exit 1
fi

. "$sbin/snappy-config.sh"
. "$sbin/spark-config.sh"

. "$SPARK_PREFIX"/bin/load-spark-env.sh


JAR_PATH="${SPARK_PREFIX}/lib"

JAR_COUNT=0
for f in "${JAR_PATH}"/quickstart.jar; do
  if [[ ! -e "$f" ]]; then
    echo "Failed to find snappy examples assembly in ${SPARK_PREFIX}/lib" 1>&2
    echo "You need to build snappydata before running this program" 1>&2
    exit 1
  fi
  SNAPPY_EXAMPLES_JAR="$f"
  JAR_COUNT=$((JAR_COUNT+1))
done

if [ "$JAR_COUNT" -gt "1" ]; then
  echo "Found multiple snappy examples assembly jars in ${JAR_PATH}" 1>&2
  ls "${JAR_PATH}"/quickstart.jar 1>&2
  echo "Please remove all but one jar." 1>&2
  exit 1
fi

export SNAPPY_EXAMPLES_JAR
APP_PROPS=${APP_PROPS:-none}

if [[ ! $SNAPPY_EXAMPLE_CLASS == io.snappydata.examples* ]]; then
  SNAPPY_EXAMPLE_CLASS="io.snappydata.examples.$SNAPPY_EXAMPLE_CLASS"
fi

output(){

"$SPARK_PREFIX"/bin/snappy-job.sh submit \
     --stream \
     --lead $LEADURL \
     --app-name $APP_NAME \
     --class $SNAPPY_EXAMPLE_CLASS \
     --app-jar "$SNAPPY_EXAMPLES_JAR" 2>&1 &

sleep 1
AWK=`which nawk 2>/dev/null`
DIRECTORY=${SPARK_PREFIX}/work/localhost-lead-1
OUTPUTFILE=`ls -tr $DIRECTORY | tail -n 2 | head -n 1 | ${AWK:-awk} '{printf $1;}'`
if [ -d ${DIRECTORY} ] 
then
  if [ ${APP_PROPS:-none} == "none" ]
  then
    echo -e "\n\nRunning example with file stream\n"
    sleep 2
    "$sbin"/../quickstart/scripts/simulateTwitterStream >/dev/null &
  else
    echo -e "\n\nRunning example with live twitter stream\n"
    sleep 3
  fi
  while [ ${OUTPUTFILE: -4} != ".out" ]
  do
    OUTPUTFILE=`ls -tr $DIRECTORY | tail -n 2 | head -n 1 | ${AWK:-awk} '{printf $1;}'`
  done 
  tail -f -F ${DIRECTORY}/${OUTPUTFILE}  2>/dev/null &
  tail_pid=$!
  sleep 60
  kill $tail_pid 
  echo -e "\n\n${yellow}Output can be found in ${DIRECTORY}/${OUTPUTFILE}${NC}"  2>/dev/null
fi
}
output
